const {
  getUserInfo,
  getLoginToken,
  getCode,
  getWxRunData,
  getJingang,
  getNotice,
  getBanner,
  getActivity,
} = require("../../utils/request/Api/user");

async function getNowPageInitData() {
  const kingData = await getJingang();
  const noticeData = await getNotice();
  const getBannerData = await getBanner();
  const getActivityData = await getActivity();

  return {
    kingData,
    noticeData,
    getBannerData,
    getActivityData,
  }
}

Page({
  data: {
    pageArray: [
      {
        t: "瀑布流(图片自高适应)",
        u: "/pages/example/waterfall/waterfall",
      },
      {
        t: "商品作用关联菜单",
        u: "/pages/example/shoplist/shoplist",
      },
      {
        t: "页面触底数据刷新",
        u: "/pages/example/rech/rech",
      },
      {
        t: "局部区域触底刷新",
        u: "/pages/example/rechview/rechview",
      },
      {
        t: "顶部下拉刷新页面数据",
        u: "/pages/example/pullrefresh/pullrefresh",
      },
      {
        t: "购物车数据携带",
        u: "/pages/example/shopcar/shopcar",
      },
      {
        t: "电子签名(较难,直接复制使用)",
        u: "/pages/example/sign/sign",
      },
      {
        t: "富文本",
        u: "/pages/example/richText/richText",
      },
      {
        t: "图表",
        u: "/pages/example/charts/charts",
      },
      {
        t: "路由跳转传参",
        u: "/pages/example/naviparams/naviparams?id=" + new Date().getTime(),
      },
      {
        t: "抽奖转盘",
        u: "/pages/example/lottery/lottery",
      },
      {
        t: "手风琴下拉",
        u: "/pages/example/accordion/accordion",
      },
      {
        t: "日历",
        u: "/pages/example/cacle/cacle",
      },
      {
        t: "demo1",
        u: "/pages/example/step/step",
      },
      {
        t: "demo2",
        u: "/pages/example/demo2/demo2",
      },
    ],
    rundata: [
      {
        name: 1,
        age: 23,
      },
    ],
    active: 1
  },
  toUrl(e) {
    const url = e.target.dataset.url;
    // if (!wx.getStorageSync("token")) {
    //   wx.getSetting({
    //     scope: "scope.werun",
    //     success: (res) => {
    //       console.log(res);
    //       if (!res.authSetting["scope.werun"]) {
    //         wx.showModal({
    //           title: "请授权",
    //           complete: (res) => {
    //             if (res.confirm) {
    //               wx.openSetting();
    //               wx.setStorageSync("into", 1);
    //             }
    //           },
    //         });
    //       }
    //     },
    //   });

    //   return;
    // }
    wx.navigateTo({
      url: url,
    });
  },
  async onLoad() {
    // wx.login 获取登录凭证 code
    const code = await getCode();
    const { encryptedData, iv } = await getWxRunData();

    getLoginToken({
      code,
      encryptedData,
      iv,
    }).then((res) => {
      wx.setStorageSync("token", res.token);
    });

    return;
  },
  onShow() {
    // if (wx.getStorageSync("into")) {
    //   wx.login({
    //     success: (code) => {
    //       // console.log(code);
    //       // 获取微信运动步数
    //       wx.getWeRunData({
    //         success: (rundata) => {
    //           // console.log(rundata, "=======");
    //           getLoginToken({
    //             code: code.code,
    //             encryptedData: rundata.encryptedData,
    //             iv: rundata.iv,
    //           }).then((res) => {
    //             wx.setStorageSync("token", res.token);
    //             wx.removeStorageSync("into");
    //           });
    //         },
    //         fail: (err) => {
    //           console.log(err);
    //         },
    //       });
    //     },
    //   });
    // }
    this.getData()
  },







  async getData(){
    const { kingData, noticeData, getBannerData, getActivityData } = await getNowPageInitData()
    this.setData({
      kingData,
      noticeData,
      getBannerData,
      getActivityData,
    })


  }
});
